package org.huawei260.TiKu.ODsuanfa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: qingle
 * @Date: 2024/10/28-16:11
 * @Description: 2815 【哈希表】2023A字符串重新排序
 * @version: 1.0
 */
import java.util.*;
public class A_2023A_2815__哈希表字符串重新排序 {

//	public class Main {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			String[] arr = sc.nextLine().split(" ");
			System.out.println(sortWords(arr));
		}

		private static String sortWords(String[] words) {
			// 单词内部排序
			for (int i = 0; i < words.length; i++) {
				char[] chars = words[i].toCharArray();
				Arrays.sort(chars);
				words[i] = new String(chars);
			}

			// 单词间排序
			Map<String, Integer> wordCount = new HashMap<>();
			for (String word : words) {
				wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
			}

			List<Map.Entry<String, Integer>> entries = new ArrayList<>(wordCount.entrySet());
			entries.sort((a, b) -> {
				if (!a.getValue().equals(b.getValue())) {
					return b.getValue() - a.getValue(); // 次数降序
				} else if (a.getKey().length() != b.getKey().length()) {
					return a.getKey().length() - b.getKey().length(); // 长度升序
				} else {
					return a.getKey().compareTo(b.getKey()); // 字典序升序
				}
			});

			// 构建最终的字符串
			StringBuilder sortedString = new StringBuilder();
			for (Map.Entry<String, Integer> entry : entries) {
				for (int i = 0; i < entry.getValue(); i++) {
					sortedString.append(entry.getKey()).append(" ");
				}
			}
			return sortedString.toString().trim();
		}
//	}
}
